package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.sys.dao.SysDeptDao;
import cn.school.sys.entity.SysDept;
import cn.school.workflow.dao.GoodsBuyApplyDao;
import cn.school.workflow.service.GoodsBuyApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.GoodsBuyApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class GoodsBuyApplyServiceImpl extends ServiceImpl<GoodsBuyApplyDao, GoodsBuyApplyVO> implements GoodsBuyApplyService {

    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    @Autowired
    private SysDeptDao sysDeptDao;

    @Override
    public IPage<GoodsBuyApplyVO> findGoodsBuyApplyPageList(GoodsBuyApplyVO vo) {
        LambdaQueryWrapper<GoodsBuyApplyVO> wrapper = new LambdaQueryWrapper<>();
        //默认查询自己提交的申请
        wrapper.eq(GoodsBuyApplyVO::getCreateBy, SysUserUtils.getSysUser().getUsername());
        if(!StringUtils.isEmpty(vo.getApplyNo())){
            wrapper.eq(GoodsBuyApplyVO::getApplyNo,vo.getApplyNo());
        }
        if(!StringUtils.isEmpty(vo.getGoodsName())){
            wrapper.like(GoodsBuyApplyVO::getGoodsName,vo.getGoodsName());
        }
        if(!StringUtils.isEmpty(vo.getCreateTime())){
            wrapper.ge(GoodsBuyApplyVO::getCreateTime,vo.getCreateTime()+ " 00:00:00");
            wrapper.le(GoodsBuyApplyVO::getCreateTime, vo.getCreateTime()+ " 23:59:59");
        }
        //租户
        wrapper.eq(GoodsBuyApplyVO::getTenantId,SysUserUtils.getSysUser().getTenantId());
        wrapper.orderByDesc(GoodsBuyApplyVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public boolean cancelGoodsBuyApply(WorkFlowVO vo) {
        LambdaQueryWrapper<GoodsBuyApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsBuyApplyVO::getProcessId,vo.getProcessInstanceId());
        GoodsBuyApplyVO applyVO = new GoodsBuyApplyVO();
        applyVO.setApplyStatus("4");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());
    }

    @Override
    public int createGoodsBuyApply(GoodsBuyApplyVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        SysDept sysdept = sysDeptDao.selectById(vo.getDeptId());
        ///查询下级处理人
        List<String> approvalUser = getApprovalUser(vo);
        String applyNo = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setDeptName(sysdept.getName());
        vo.setApplyNo("GB"+applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        vo.setCurrAppprovedBy(approvalUser.get(0));
        vo.setApplyStatus("1");//提交后默认为待审批状态
        //创建申请单
        this.baseMapper.insert(vo);

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("物品申购申请");
        flowVO.setApplyID("goodsBuyApply");
        flowVO.setBusinessKey(applyNo);
        //查询下级处理人
        Map<String,Object> map =new HashMap<>();
        map.put("assignee",approvalUser.get(0));
        flowVO.setVariables(map);
        //创建申购工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);
    }
    /**
     * 获取选择部门的审批人
     * @param vo
     * @return
     */
    private List<String> getApprovalUser(GoodsBuyApplyVO vo) {
        String deptStr = vo.getDeptId();
        List<Long> deptList= new ArrayList<>();
        deptList.add(Long.valueOf(deptStr));

        List<String> approvalUser = workFlowCommonService.findAssigneeList(deptList);
        return approvalUser;
    }

    @Override
    public GoodsBuyApplyVO findGoodsBuyApplyDetail(Long processInstanceId) {
        LambdaQueryWrapper<GoodsBuyApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsBuyApplyVO::getProcessId,processInstanceId);
        return this.baseMapper.selectOne(wrapper);
    }
}
